import { ElMessage } from 'element-plus'

let messageInstance: any = null

// 重置message，防止多次点击重复弹出message弹框
const resetMessage: any = (options: any) => {
  if (messageInstance) {
    messageInstance.close()
  }
  messageInstance = ElMessage(options)
}

const typesArr = ['error', 'success', 'info', 'warning']

typesArr.forEach(type => {
  resetMessage[type] = (options: any) => {
    if (typeof options === 'string') {
      options = {
        message: options
      }
    }
    options.type = type
    return resetMessage(options)
  }
})
export const useSingleMessage = resetMessage
